/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include "CQuaternion.h"

#include <math.h>

//Constructors
CQuaternion::CQuaternion()
{
    mData[0] = 0.0f;
    mData[1] = 0.0f;
    mData[2] = 0.0f;
	mData[3] = 0.0f;
}

CQuaternion::CQuaternion(float a, float x, float y, float z)
{
    mData[0] = a;
    mData[1] = x;
    mData[2] = y;
	mData[3] = z;
}

CQuaternion::CQuaternion(float components[4])
{
    mData[0] = components[0];
    mData[1] = components[1];
    mData[2] = components[2];
	mData[3] = components[3];
}

CQuaternion::CQuaternion(float scalar, CVector3 vector, bool isRotation)
{
	if (!isRotation)
	{
		mData[0] = scalar;
		mData[1] = vector.x();
		mData[2] = vector.y();
		mData[3] = vector.z();
	}
	else
	{
		//in this case scalar is an angle in radians
		mData[0] = cos( scalar / float(2.0) );

		float sinHalfAngle = sin( scalar / float(2.0) );

		// make sure rotation vector is a unit vector
		CVector3 rotV = vector.Normalize();

		mData[1] = rotV.x() * sinHalfAngle;
		mData[2] = rotV.y() * sinHalfAngle;
		mData[3] = rotV.z() * sinHalfAngle;
	}
}

CQuaternion::CQuaternion(const CQuaternion &source)
{
    mData[0] = source.mData[0];
    mData[1] = source.mData[1];
    mData[2] = source.mData[2];
	mData[3] = source.mData[3];
}

CQuaternion::~CQuaternion()
{

}

//Opearators overloading
float& CQuaternion::operator() (const unsigned short i)
{
    //check if the requested index is out of bounds.
    if (i < 4)
        return mData[i];
    else
    {
        std::cout << "Error when calling CQuaternion operator(), requested indexes are out of bounds." << std::endl;
        return mData[0];
    }
}

CQuaternion CQuaternion::operator+ ( CQuaternion quat )
{
    CQuaternion out;

    out(0) = mData[0] + quat(0);
    out(1) = mData[1] + quat(1);
    out(2) = mData[2] + quat(2);
	out(3) = mData[3] + quat(3);
	
    return out;
}

CQuaternion CQuaternion::operator+ (const float scalar)
{
    CQuaternion out;

    out(0) = mData[0] + scalar;
    out(1) = mData[1];
    out(2) = mData[2];
	out(3) = mData[3];

    return out;
}

CQuaternion CQuaternion::operator- ( CQuaternion quat )
{
    CQuaternion out;

    out(0) = mData[0] - quat(0);
    out(1) = mData[1] - quat(1);
    out(2) = mData[2] - quat(2);
	out(3) = mData[3] - quat(3);

    return out; 
}

CQuaternion CQuaternion::operator- (const float scalar)
{
    CQuaternion out;

    out(0) = mData[0] - scalar;
    out(1) = mData[1];
    out(2) = mData[2];
	out(3) = mData[3];

    return out; 
}

CQuaternion CQuaternion::operator* ( CQuaternion quat )
{
    CQuaternion out;

	out(0) = mData[0]*quat(0) - mData[1]*quat(1) - mData[2]*quat(2) - mData[3]*quat(3);
	out(1) = mData[0]*quat(1) + mData[1]*quat(0) + mData[2]*quat(3) - mData[3]*quat(2);
	out(2) = mData[0]*quat(2) - mData[1]*quat(3) + mData[2]*quat(0) + mData[3]*quat(1);
	out(3) = mData[0]*quat(3) + mData[1]*quat(2) - mData[2]*quat(1) + mData[3]*quat(0);

    return out;
}

CQuaternion CQuaternion::operator* (CVector3 vector)
{
	CQuaternion out;

	out(0) = - mData[1]*vector(0) - mData[2]*vector(1) - mData[3]*vector(2);
	out(1) = mData[0]*vector(0) + mData[2]*vector(2) - mData[3]*vector(1);
	out(2) = mData[0]*vector(1) - mData[1]*vector(2) + mData[3]*vector(0);
	out(3) = mData[0]*vector(2) + mData[1]*vector(1) - mData[2]*vector(0);

    return out;
}

CQuaternion CQuaternion::operator* ( const float scalar )
{
    CQuaternion out;

	out(0) = mData[0]*scalar;
	out(1) = mData[1]*scalar;
	out(2) = mData[2]*scalar;
	out(3) = mData[3]*scalar;

    return out; 
}

CQuaternion& CQuaternion::operator= ( CQuaternion quat )
{
    //if the same instance as "this" is passed as argument, return this without performing
    //any operation
    if (this == &quat)
        return *this;

    mData[0] = quat(0);
    mData[1] = quat(1);
    mData[2] = quat(2);
	mData[3] = quat(3);

    return *this;
}

bool CQuaternion::operator== ( CQuaternion quat )
{
    if ( (mData[0] == quat(0)) &&
         (mData[1] == quat(1)) &&
         (mData[2] == quat(2)) && 
		 (mData[3] == quat(3)) )
        return true;
    else
        return false;
}

bool CQuaternion::operator!= (CQuaternion quat)
{
    bool ret = false;

    if ( ( mData[0] == quat(0) ) && ( mData[1] == quat(1) ) && ( mData[2] == quat(2) ) 
		&& ( mData[3] == quat(3) ))
        ret = true;

    return ret;
}

//other functions
float CQuaternion::Norm()
{
    return sqrt( mData[0]*mData[0] + mData[1]*mData[1] + mData[2]*mData[2] + mData[3]*mData[3] );
}

void CQuaternion::Normalize()
{
	mData[0] /= Norm();
	mData[1] /= Norm();
	mData[2] /= Norm();
	mData[3] /= Norm();
}

void CQuaternion::SetComponents(float a, float x, float y, float z)
{
	mData[0] = a;
    mData[1] = x;
    mData[2] = y;
    mData[3] = z;
}

void CQuaternion::SetComponents(float a, CVector3 vector)
{
	mData[0] = a;
	mData[1] = vector.x();
    mData[2] = vector.y();
	mData[3] = vector.z();
}

void CQuaternion::SetRotationComponents(float angle, CVector3 rotationAxis)
{
	mData[0] = cos( angle / float(2.0) );

	float sinHalfAngle = sin( angle / float(2.0) );

	// make sure rotation vector is a unit vector
	CVector3 rotV = rotationAxis.Normalize();

	mData[1] = rotV.x() * sinHalfAngle;
	mData[2] = rotV.y() * sinHalfAngle;
	mData[3] = rotV.z() * sinHalfAngle;
}

float* CQuaternion::GetComponents()
{
    return mData;
}

float& CQuaternion::GetScalarPart()
{
	return mData[0];
}

CVector3 CQuaternion::GetVectorPart()
{
	return CVector3(mData[1], mData[2], mData[3]);
}

CQuaternion CQuaternion::Conjugate()
{
	return CQuaternion(mData[0], -mData[1], -mData[2], -mData[3]);
}

CVector3 CQuaternion::RotateVector(CVector3 vector)
{
	CQuaternion tempQ;
	CQuaternion conjQ = Conjugate();

	tempQ = (*this)*vector*conjQ;

	return tempQ.GetVectorPart();
}

std::ostream & operator<< (std::ostream &out, CQuaternion &quat)
{
	out << quat.mData[0] << " " << quat.mData[1] << " " << quat.mData[2] << " " << quat.mData[3];
	return out;
}

